package com.loongtech.bi.synch;

import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.bi.manager.system.BiServerManager;
import com.loongtech.bi.manager.system.ConfigManager;
import com.loongtech.bi.support.Log;
import com.loongtech.core.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * 管理和游戏服务器的参数同步
 * @author xckuang
 * @date 2014年12月19日
 */
@Service
public class DataSyncher
{
	private String					gameId		= "203";
	private String				serverURL		= "";
	
	// 获取gameId的名称
	public static final String	TagGameId		= "eGameId";
	// 获取服务器列表
    private static final String	TagServerURL	= "eServerURL";
	@Autowired
	private BiServerManager		serverManager;
	@Autowired
	private BiPlatformManager 	platformManager;
	@Autowired
	private ConfigManager		configManager;

	public void syncData()
	{
		gameId    = configManager.getConfigValueByName(TagGameId);
		serverURL = configManager.getConfigValueByName(TagServerURL);
		
		//判断gameId
		if(null == gameId || gameId.trim().isEmpty()){
			Log.bi.error("DataSyncher syncData gameId is error !");
			return;
		}
		
		Log.bi.error("DataSyncher syncData gameId="+gameId);
		
		//判断serverURL
		if(null == serverURL || serverURL.trim().isEmpty()){
			Log.bi.error("DataSyncher syncData serverURL is error !");
			return;
		}	
		
		Log.bi.error("DataSyncher syncData serverURL="+serverURL);
		
		//http获取信息
		try {
			String serverInfo = Utils.httpGet(serverURL, null);
            DataManager.instance().updateServer(serverManager, platformManager, serverInfo);
		} catch (IOException ex) {
			ex.printStackTrace();
            Log.bi.error("服务器信息获取失败:" + ex);
			return;
		}		
	} 
}
